import hashlib
import base64
import random
import string


class UserService():

    @staticmethod
    def gene_AuthCode(user_info):
        m = hashlib.md5()
        str = "%s-%s-%s-%s" % (user_info.uid,
                               user_info.login_name,
                               user_info.login_pwd,
                               user_info.login_salt)
        m.update(str.encode("utf-8"))
        return m.hexdigest()

    # 用户输入的密码密文显示
    @staticmethod
    def gene_pwd(pwd, salt):
        m = hashlib.md5()
        str = "%s-%s" % (base64.encodebytes(pwd.encode("utf-8")), salt)
        m.update(str.encode("utf-8"))
        return m.hexdigest()

    @staticmethod
    def gen_salt(length=16):
        key_list = [random.choice((string.ascii_letters + string.digits)) for i in range(length)]
        return ("".join(key_list))